package com.loongtech.bi.action.system;

import com.loongtech.bi.entity.system.EntitySysRole;
import com.loongtech.bi.entity.system.EntitySysUser;
import com.loongtech.bi.entity.system.EntitySysUserRole;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.manager.system.SysRoleManager;
import com.loongtech.bi.manager.system.SysUserManager;
import com.loongtech.bi.manager.system.SysUserProjectRoleManager;
import com.loongtech.bi.support.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.io.Serializable;
import java.util.*;

/**
 * 单个角色用户设置
 * 
 * @author xckuang
 * @date 2014-10-08
 */
@Controller("roleUserManager")
@Scope("view")
public class RoleUserManager implements Serializable {
	private static final long serialVersionUID = 1L;
	@Autowired
	private Session session;
	@Autowired
	private SysLogManager sysLogManager;
	@Autowired
	private SysRoleManager sysRoleManager;
	@Autowired
	private SysUserManager sysUserManager;
	@Autowired
	private SysUserProjectRoleManager sysUserProjectRoleManager;
	
	private Map<Integer, Boolean> selected = new HashMap<Integer, Boolean>();
	private Map<Integer, Boolean> unselected = new HashMap<Integer, Boolean>();
	private int roleId;
	private Map<Integer, EntitySysRole> roleMap = new HashMap<Integer, EntitySysRole>();
	private List<EntitySysUser> leftList = new ArrayList<EntitySysUser>(); //可选择列表
	private List<EntitySysUser> rightList = new ArrayList<EntitySysUser>();//已经选择列表

	@Autowired
	public void init() {		
		roleMap.clear();
		roleId = 0;
		for(EntitySysRole role : sysRoleManager.findAll()){
			roleMap.put(role.getId(), role);
		}	
		change();
	}

	public void select() {
		for (EntitySysUser user : leftList) {
			Boolean val = selected.get(user.getId());
			if (null != val && val.booleanValue()) {
				rightList.add(user);
			}
		}
		selected.clear();
		leftList.removeAll(rightList);
	}

	public void unSelect() {
		for (EntitySysUser user : rightList) {
			Boolean val = unselected.get(user.getId());
			if (null != val && val.booleanValue()) {
				leftList.add(user);
			}
		}
		unselected.clear();
		rightList.removeAll(leftList);
	}

	/**
	 * 保存角色用户信息
	 */
	public void save() {	
		FacesContext context = FacesContext.getCurrentInstance();
		EntitySysRole role = sysRoleManager.get(roleId);
		if(null == role){
			context.addMessage(null, new FacesMessage("修改角色用户列表失败", "修改角色用户列表失败，角色不存在 roleId="+roleId));
			return;
		}
		// 当前集合
		List<EntitySysUserRole> list = sysUserProjectRoleManager.findBy(EntitySysUserRole.K.roleId, roleId);
		Set<Integer> oldSet = new HashSet<Integer>();
		if (null != list && !list.isEmpty()) {
			for (EntitySysUserRole info : list) {
				oldSet.add(info.getUserId());
			}
		}
		// newSet
		Set<Integer> newSet = getSet(rightList);
		Set<Integer> submit = new HashSet<Integer>(oldSet);
		submit.retainAll(newSet);
		oldSet.removeAll(submit); //to remove
		newSet.removeAll(submit); //to insert
		
		// 删除oldSet
		for(int userId : oldSet){
			EntitySysUserRole entity = sysUserProjectRoleManager.getBy(EntitySysUserRole.K.roleId, roleId, EntitySysUserRole.K.userId, userId);
			if(null != entity){
				sysUserProjectRoleManager.removeById(entity.getId());
			}
		}
		
		// 插入newSet
		for (int userId : newSet) {
			if (null == sysUserProjectRoleManager.getBy(EntitySysUserRole.K.roleId, roleId, EntitySysUserRole.K.userId, userId)) {
				EntitySysUserRole entity = new EntitySysUserRole();
				entity.setRoleId(roleId);
				entity.setUserId(userId);
				sysUserProjectRoleManager.persist(entity);
			}
		}
		sysLogManager.addLog(session.getUserName(), "修改角色用户信息列表成功"+role.getName());
		context.addMessage(null, new FacesMessage("修改角色用户信息列表成功", "修改角色用户信息列表成功，角色名称为="+role.getName()));
		change();	
	}
	
	private Set<Integer> getSet(List<EntitySysUser> list) {
		Set<Integer> curSet = new HashSet<Integer>();
		if (null != list && !list.isEmpty()) {
			for (EntitySysUser info : list) {
				if (null != sysUserManager.get(info.getId())) {
					curSet.add(info.getId());
				}
			}
		}
		return curSet;
	}
	
	public void change() {
		selected.clear();
		unselected.clear();
		EntitySysRole role = sysRoleManager.get(roleId);
		if (null == role) {
			leftList.clear();
			rightList.clear();
			leftList.addAll(sysUserManager.findAll());			
			return;
		}
		
		//先初始化rightList
		rightList.clear();		
		List<EntitySysUserRole> list = sysUserProjectRoleManager.findBy(EntitySysUserRole.K.roleId, roleId);
		if (null != list && !list.isEmpty()) {
			for (EntitySysUserRole info : list) {
				EntitySysUser user = sysUserManager.get(info.getUserId());
				if (null != user) {
					rightList.add(user);
				}
			}
		}
		
		leftList.clear();
		leftList.addAll(sysUserManager.findAll());
		leftList.removeAll(rightList);
	}

	public int getRoleId() {
		return roleId;
	}

	public void setRoleId(int roleId) {
		this.roleId = roleId;
	}

	public Map<Integer, EntitySysRole> getRoleMap() {
		return roleMap;
	}

	public void setRoleMap(Map<Integer, EntitySysRole> roleMap) {
		this.roleMap = roleMap;
	}

	public Map<Integer, Boolean> getSelected() {
		return selected;
	}

	public void setSelected(Map<Integer, Boolean> selected) {
		this.selected = selected;
	}

	public Map<Integer, Boolean> getUnselected() {
		return unselected;
	}

	public void setUnselected(Map<Integer, Boolean> unselected) {
		this.unselected = unselected;
	}

	public List<EntitySysUser> getLeftList() {
		return leftList;
	}

	public void setLeftList(List<EntitySysUser> leftList) {
		this.leftList = leftList;
	}

	public List<EntitySysUser> getRightList() {
		return rightList;
	}

	public void setRightList(List<EntitySysUser> rightList) {
		this.rightList = rightList;
	}
}